<?php 
// 发送最终确认
/**
参数列表：
    	$param['mid'] = $uid;
    	$param['email'] = $uinfo['email'];
    	$param['ischeck'] = $ischeck;
 */

//**************************************通用环境定义开始************************//
set_time_limit(0);
ini_set("memory_limit", -1);
define ('APP', dirname(dirname(__FILE__)));

require_once APP . '/frame/Route.php';
require_once FRAME_ROOT_PATH . '/db/Mysql.php';
require_once APP . '/application/configs/application.php';
require_once APP . '/application/models/mail/class.phpmailer.php';

global $cfg;
Mysql::setInstance("default", $cfg['db']['host'], $cfg['db']['user'], $cfg['db']['password'], $cfg['db']['name'], $cfg['db']['charset']);
if ($cfg ['debug'])
    Mysql::getInstance()->setProfiler(true);

$db = Mysql::getInstance();
$str_args = $argv[1];
$arr_args = unserialize(base64_decode($str_args));

//clog(print_R($arr_args, true));
//**************************************定义结束************************//

// 获取会议信息
$arr_meet = $db->fetchRow("select * from meet where mid=?", array($arr_args['mid']));

$mail = new PHPMailer();

//clog(print_R($arr_meet, true));
$arr_mail_log = array();
$arr_mail_log['type'] = 'status';

$arr_mail_log['sendargs'] = array();
$arr_mail_log['sendargs']['smtpserver'] = $arr_meet['smtpserver'];
$arr_mail_log['sendargs']['email'] = $arr_meet['email'];
$arr_mail_log['sendargs']['emailpwd'] = $arr_meet['emailpwd'];
$arr_mail_log['sendargs']['emailuser'] = $arr_meet['emailuser'];

$arr_mail_log['sendargs'] = serialize($arr_mail_log['sendargs']);

$arr_mail_log['mid'] = $arr_args['mid'];

$mail->SMTPDebug = true;
$mail->SMTPKeepAlive = true;
$mail->IsSMTP();					// 启用SMTP
$mail->Host = $arr_meet['smtpserver'];			//SMTP服务器
$mail->SMTPAuth = true;					//开启SMTP认证
$mail->Username = $arr_meet['email'];			// SMTP用户名
$mail->Password = $arr_meet['emailpwd'];				// SMTP密码
$mail->From = $arr_meet['email'];;			//发件人地址
$mail->FromName = $arr_meet['emailuser'];;				//发件人

$mail->WordWrap = 80;					//设置每行字符长度

$mail->CharSet = "utf-8";				// 这里指定字符集！
$mail->Encoding = "base64";
$mail->IsHTML(true);					// 是否HTML格式邮件


$meeturl = $arr_args['url'];
$uinfo = $db->fetchRow("select * from user where uid=?", array($arr_args['uid']));
$arr_status = array('未提交', "待审核", "通过审核", "未通过审核", "最终确认");
$arr_status_en = array('To Be Submitted', "To Be Verified", "Already Verified", "Not Passed", "Finally Confirmed");

if ($arr_meet['language'] == 2){
	$title = "Registration Status";
	$status = $arr_status[$arr_args['ischeck']];
	$content = '<br/><br/>您在"'.$arr_meet['name'].'" 的注册状态变更为"'.$status.'"，请登录 <a href="'.$meeturl.'?lan=cn">'.$meeturl.'</a> 查看。';
	
	$status = $arr_status_en[$arr_args['ischeck']];
	$content .='<br /><br />Your registration status at "'.$arr_meet['enname'].'" is changed as "'.$status.'", please log on <a href="'.$meeturl.'?lan=en">'.$meeturl.'</a> to view.';
	$content .= '<br /><p>&nbsp;</p>--------------------<p>'.$arr_meet['name'].'会议注册系统：<a href="'.$meeturl.'?lan=cn">'.$meeturl.'</a>';
	$content .= '<br />'.$arr_meet['enname'].' Conference Service Platform: <a href="'.$meeturl.'?lan=en">'.$meeturl.'</a></p>';
}else{
	$title = "注册状态";
	$status = $arr_status[$arr_args['ischeck']];
	$content = '<br/><br/>您在"'.$arr_meet['name'].'" 的注册状态变更为 "'.$status.'"，请登录 <a href="'.$meeturl.'">'.$meeturl.'</a> 查看。';
	$content .= '<br /><p>&nbsp;</p>--------------------<p>'.$arr_meet['name'].'会议注册系统：<a href="'.$meeturl.'">'.$meeturl.'</a></p>';
}

$mail->Subject = $title;			//邮件主题
$mail->Body = $content;
$mail->ClearAddresses();
$mail->AddAddress($arr_args['email']);

// 发送成功
$ret = "ok";
try{
	if(!$mail->Send()){
		$ret = $mail->ErrorInfo;
	}
}catch(Exception $e){
	$ret = $e->getMessage();
}	

// 记录发送日志
$arr_mail_log['sendtime'] = date("Y-m-d H:i:s", time());
$arr_mail_log['content'] = $content;
$arr_mail_log['title'] = $title;
$arr_mail_log['toemail'] = $arr_args['email'];
$arr_mail_log['message'] = $ret;

$db->insert("mailog", $arr_mail_log);

$mail->SmtpClose();


function clog($text)
{
	file_put_contents(APP . "/cache/a.log", $text, FILE_APPEND);
}
?>